package cn.sycoder.controller;

import cn.sycoder.service.RedisService;
import cn.sycoder.utils.JwtUtil;
import cn.sycoder.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@Api(tags = "测试接口")
@RequestMapping("/test")
@Transactional(rollbackFor = Exception.class)
public class TestController {

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("api/test/set")
    @ApiOperation(value = "验证 存储redis")
    public boolean setRedis(@RequestParam(value = "id" ,required = false)Long id){
        redisUtil.set(id.toString(),"hello Redis");
        return true;
    }

    @GetMapping("api/test/get")
    @ApiOperation(value = "验证 获取redis")
    public String getRedis(@RequestParam(value = "id" ,required = false)Long id){
        return redisUtil.get(id.toString()).toString();
    }

    @PostMapping
    @ApiOperation(value = "Token有效性验证")
    public Map<String, String> validateToken(@RequestParam String token, @RequestParam String username) {
        Map<String, String> response = new HashMap<>();

        if (JwtUtil.verifyToken(token, username)) {
            response.put("message", "Token有效");
        } else {
            response.put("message", "Token无效或已过期");
        }

        return response;
    }
}
